// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/foundation.dart' show listEquals;
import 'package:flutter/material.dart' show Color;
import 'package:amap_flutter_base/amap_flutter_base.dart';
import 'base_overlay.dart';
import 'polyline.dart';

class Circle extends BaseOverlay {
  /// 默认构造函数
  Circle({required this.center, required this.radius, double strokeWidth = 1, this.strokeColor = const Color(0xCC00BFFF), this.fillColor = const Color(0xC487CEFA), this.visible = true, this.zIndex = 0.0}) : this.strokeWidth = (strokeWidth <= 0 ? 1 : strokeWidth), super();

  /// 覆盖物的坐标点数组,不能为空
  final LatLng center;

  /// 边框宽度,单位为逻辑像素，同Android中的dp，iOS中的point
  final double strokeWidth;

  /// 边框颜色,默认值为(0xCCC4E0F0)
  final Color strokeColor;

  /// 填充颜色,默认值为(0xC4E0F0CC)
  final Color fillColor;

  /// 是否可见
  final bool visible;

  final double radius;

  /// z轴的值，用于调整该覆盖物的相对绘制层级关系
  /// 值越小，图层越靠下，iOS该值不支持动态修改,仅能在初始化时指定
  final double zIndex;

  /// 实际copy函数
  Circle copyWith({LatLng? centerParam, double? radiusParam, double? strokeWidthParam, Color? strokeColorParam, Color? fillColorParam, bool? visibleParam}) {
    Circle copyCircle = Circle(center: centerParam ?? center, strokeWidth: strokeWidthParam ?? strokeWidth, strokeColor: strokeColorParam ?? strokeColor, fillColor: fillColorParam ?? fillColor, visible: visibleParam ?? visible, radius: radiusParam ?? radius, zIndex: zIndex);
    copyCircle.setIdForCopy(id);
    return copyCircle;
  }

  Circle clone() => copyWith();

  /// 转换成可以序列化的map
  @override
  Map<String, dynamic> toMap() {
    final Map<String, dynamic> json = <String, dynamic>{};

    void addIfPresent(String fieldName, dynamic value) {
      if (value != null) {
        json[fieldName] = value;
      }
    }

    addIfPresent('id', id);
    addIfPresent('center', center.toJson());
    addIfPresent('strokeWidth', strokeWidth);
    addIfPresent('strokeColor', strokeColor.value);
    addIfPresent('fillColor', fillColor.value);
    addIfPresent('visible', visible);
    addIfPresent('radius', radius);
    addIfPresent('zIndex', zIndex);
    return json;
  }

  @override
  bool operator ==(Object other) {
    if (identical(this, other)) return true;
    if (other.runtimeType != runtimeType) return false;
    if (other is! Circle) return false;
    final Circle typedOther = other;
    return id == typedOther.id && center == typedOther.center && strokeWidth == typedOther.strokeWidth && strokeColor == typedOther.strokeColor && fillColor == typedOther.fillColor && visible == typedOther.visible && radius == typedOther.radius && zIndex == typedOther.zIndex;
  }

  @override
  int get hashCode => super.hashCode;
}

Map<String, Circle> keyByCircleId(Iterable<Circle> circles) {
  return Map<String, Circle>.fromEntries(circles.map((Circle circle) => MapEntry<String, Circle>(circle.id, circle.clone())));
}
